package com.lbd.gulimall.cart.vo;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: linbd
 * @Description:
 * @Date: 2023/3/23 22:20
 */
public class CartVO {
    private List<CartItemVO> cartItems;
    private Integer cartCount = 0;
    private Integer cartTypeCount = 0;
    private BigDecimal cartTotalPrice = new BigDecimal(0);
    private BigDecimal reducePrice = new BigDecimal(0);

    public List<CartItemVO> getCartItems() {
        return cartItems;
    }

    public void setCartItems(List<CartItemVO> cartItems) {
        this.cartItems = cartItems;
    }

    public Integer getCartCount() {
        List<CartItemVO> cartItems1 = getCartItems();
        if (cartItems1 != null && cartItems1.size() > 0) {
            cartCount = Math.toIntExact(getCartItems().stream().collect(Collectors.summarizingInt(CartItemVO::getCount)).getCount());
        }
        return cartCount;
    }

    public Integer getCartTypeCount() {
        List<CartItemVO> cartItems1 = getCartItems();
        if (cartItems1 != null && cartItems1.size() > 0) {
            cartTypeCount = Math.toIntExact(getCartItems().stream().collect(Collectors.counting()));
        }
        return cartTypeCount;
    }

    public BigDecimal getCartTotalPrice() {
        List<CartItemVO> cartItems1 = getCartItems();
        if (cartItems1 != null && cartItems1.size() > 0) {
            BigDecimal cartTotalPrice = cartItems1.stream().filter(CartItemVO::getChecked).map(CartItemVO::getTotalPrice).reduce(new BigDecimal(0), (x, y) -> x.add(y));
            this.cartTotalPrice = cartTotalPrice.subtract(getReducePrice());
        }
        return this.cartTotalPrice;
    }

    public BigDecimal getReducePrice() {
        return reducePrice;
    }

    public void setReducePrice(BigDecimal reducePrice) {
        this.reducePrice = reducePrice;
    }

    @Override
    public String toString() {
        return "CartVO{" +
                "cartItems=" + cartItems +
                ", cartCount=" + getCartCount() +
                ", cartTypeCount=" + getCartTypeCount() +
                ", cartTotalPrice=" + getCartTotalPrice() +
                ", reducePrice=" + reducePrice +
                '}';
    }
}
